unit uCorrCodProd;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Formulario_Padrao_Rel, DB, RxMemDS, RDprint, ExtCtrls, StdCtrls,
  CheckLst, ComCtrls, TFlatButtonUnit, Buttons, dbtables, Gauges;

type
  TfrmCorrCodProd = class(TFrmPad_Rel)
    CDS: TRxMemoryData;
    CDSProOld: TStringField;
    CDSProNew: TStringField;
    pb: TGauge;
    procedure bimprimirClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCorrCodProd: TfrmCorrCodProd;

implementation

uses Dm;

{$R *.dfm}

procedure TfrmCorrCodProd.bimprimirClick(Sender: TObject);
var
  tq, tq1: TQuery;
begin
  inherited;
  tq := TQuery.Create(self);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := False;
  tq.SQL.Clear;
  tq.SQL.Add('Select Pro_Codigo, substr(pro_codigo,1,2)||' + chr(39) + '.' + chr(39) + '||' + chr(39) + '00' + chr(39) + '||' + chr(39) + '.' + chr(39) + '||substr(pro_codigo,4,4)||' + chr(39) + '.' + chr(39) + '||substr(pro_codigo,9,1)||' + chr(39) + '.' + chr(39) + '||substr(pro_codigo,11,2)) Prod ' +
    'from Produto ');
  tq.Open;
  CDS.Close;
  CDS.Open;
  pb.MinValue := 0;
  pb.MaxValue := tq.RecordCount;
  pb.Progress := 0;
  while not tq.Eof do begin
    CDS.Append;
    CDSProOld.Value := tq.FieldByName('Pro_Codigo').AsString;
    CDSProNew.Value := tq.FieldByName('Prod').AsString;
    CDS.Post;
    tq.Next;
  end;
  tq1 := TQuery.Create(self);
  tq1.DatabaseName := DmInt.Db.DatabaseName;

  CDS.First;
  while not CDS.Eof do begin
    tq1.Active := False;
    tq1.SQL.Clear;
    tq1.Params.Clear;
    tq1.SQL.Add('Update Produto set Pro_Codigo =:PNew where Pro_Codig =:POld');
    tq1.ParamByName('PNew').AsString := CDSProNew.Value;
    tq1.ParamByName('POld').AsString := CDSProNew.Value;
    tq1.ExecSQL;

    tq1.Active := False;
    tq1.SQL.Clear;
    tq1.Params.Clear;
    tq1.SQL.Add('Update DEPARTAMENTO_PRODUTO set Pro_Codigo =:PNew where Pro_Codig =:POld');
    tq1.ParamByName('PNew').AsString := CDSProNew.Value;
    tq1.ParamByName('POld').AsString := CDSProNew.Value;
    tq1.ExecSQL;

    CDS.Next;
  end;

  tq.Close;
  tq.Free;
  tq1.Close;
  tq1.Free;
end;

end.
